package com.axiang.module_sign.viewmodel;

import android.app.Application;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.annotation.NonNull;
import android.text.Editable;
import android.view.View;

import com.axiang.lib_api.bus.RxBus;
import com.axiang.lib_common.binding.command.BindingAction;
import com.axiang.lib_common.binding.command.BindingCommand;
import com.axiang.lib_common.binding.command.BindingConsumer;
import com.axiang.lib_common.event.SingleLiveEvent;
import com.axiang.lib_common.mvvm.viewmodel.BaseViewModel;
import com.axiang.lib_common.network.CommonObserver;
import com.axiang.lib_common.utils.RxToast;
import com.axiang.lib_common.utils.StringUtils;
import com.axiang.module_sign.model.LoginModel;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginViewModel extends BaseViewModel<LoginModel> {

    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt(View.GONE);
    //密码清除按钮的显示隐藏绑定
    public ObservableInt clearPwdVisibility = new ObservableInt(View.GONE);

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
    }


    public LoginViewModel(@NonNull Application application, LoginModel model) {
        super(application, model);
    }

    //用户名输入框焦点改变的回调事件
    public BindingCommand<Editable> onUserTextChangedCommand = new BindingCommand<>(new BindingConsumer<Editable>() {
        @Override
        public void call(Editable editable) {
            if (!StringUtils.isEmpty(editable) && clearBtnVisibility.get() == View.GONE) {
                clearBtnVisibility.set(View.VISIBLE);
            } else if (StringUtils.isEmpty(editable)){
                clearBtnVisibility.set(View.GONE);
            }
        }
    });

    //用户名输入框焦点改变的回调事件
    public BindingCommand<Editable> onPwdTextChangedCommand = new BindingCommand<>(new BindingConsumer<Editable>() {
        @Override
        public void call(Editable editable) {
            if (!StringUtils.isEmpty(editable) && clearPwdVisibility.get() == View.GONE) {
                clearPwdVisibility.set(View.VISIBLE);
            } else if (StringUtils.isEmpty(editable)) {
                clearPwdVisibility.set(View.GONE);
            }
        }
    });

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });
    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearPassWordOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            password.set("");
        }
    });

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });

    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            mModel.login().subscribe(new CommonObserver<Object, LoginViewModel>(LoginViewModel.this) {
                @Override
                public void onSuccess(Object s) {
                    RxToast.success("登录成功");
                    RxBus.getDefault().post("阿祥");
                    postFinishActivityEvent();
                }
            });
        }
    });

}
